<template>
  <div class="common-page f-c">
    <el-form inline>
      <el-form-item label="院系">
        <el-select v-model="searchParams.school_id" @change="changeSchool">
          <el-option v-for="item in schoolList" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="班级">
        <el-select v-model="searchParams.class_id">
          <el-option v-for="item in classList" :key="item.id" :value="item.id" :label="item.class_name" />
        </el-select>
      </el-form-item>
      <el-form-item label="学生姓名">
        <el-input v-model="searchParams.student_name" clearable placeholder="" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="margin-left: 15px" @click="onSearch">搜索</el-button>
        <el-button type="primary" style="margin-left: 15px" @click="onReset">清除</el-button>
      </el-form-item>
    </el-form>
    <div class="button-list2 f-r a-c" style="height: 74px">
      <el-button type="danger" @click="onAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table
        v-loading="loading"
        style="width: 100%"
        height="100%"
        stripe
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column type="index" align="center" label="序号" />
        <el-table-column label="状态" width="120px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.statusName !== undefined" :type="scope.row.status === 0 ? 'danger' : 'success'">{{ scope.row.statusName }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          v-for="item in columns"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          align="center"
        />
        <el-table-column label="操作" fixed="right" width="120px">
          <template slot-scope="scope">
            <el-button
              type="text"
              style="color: #0DB493"
              @click="onEdit(scope.row)"
            > 编辑
            </el-button>
            <el-button
              type="text"
              style="color: red"
              @click="onDelete(scope.row)"
            > 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <PageAdd ref="PageAddRef" @save="emitSave" />
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import PageAdd from './page-add'
import { http } from '@/api'
import { mapGetters } from 'vuex'
export default {
  name: 'Page',
  components: {
    PageAdd
  },
  computed: {
    ...mapGetters(['schoolList'])
  },
  mixins: [crud],
  data() {
    return {
      request: http.student,
      searchParams: {
        student_name: ''
      },
      columns: [
        { prop: 'student_name', label: '学生姓名' },
        { prop: 'sex', label: '性别' },
        { prop: 'number', label: '学号' },
        { prop: 'school.school_name', label: '院系' },
        { prop: 'class.class_name', label: '班级' },
        { prop: 'created_at', label: '创建时间' }
      ],
      classList: []
    }
  },
  methods: {
    changeSchool(val) {
      http._class.list({
        school_id: val
      }).then(res => {
        this.classList = res
      })
    },
    formatTableData(data) {
      data.forEach(el => {
        if (el.status === 0) {
          el.statusName = '未激活'
        } else if (el.status === 1) {
          el.statusName = '已激活'
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.common-page {
  height:  100%;
}
</style>
